package org.ligson.hbase127.core;

import org.ligson.hbase127.reposiotry.SimpleHBaseRepository;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.util.Assert;

public class HBaseRepositoryFactory extends RepositoryFactorySupport {
    private HbaseTemplate hbaseTemplate;

    public HBaseRepositoryFactory(HbaseTemplate hbaseTemplate) {
        this.hbaseTemplate = hbaseTemplate;
    }

    @Override
    public <T, ID> HBaseEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
        return new HBaseEntityInformation<T, ID>(domainClass);
    }


    @Override
    protected Object getTargetRepository(RepositoryInformation information) {
//
        HBaseEntityInformation<?, Object> entityInformation = getEntityInformation(information.getDomainType());
        Object repository = getTargetRepositoryViaReflection(information, entityInformation, hbaseTemplate);
//
        Assert.isInstanceOf(SimpleHBaseRepository.class, repository);
//
        return (SimpleHBaseRepository<?, ?>) repository;
//        return null;
    }

    @Override
    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
        return SimpleHBaseRepository.class;
    }

    public HbaseTemplate getHbaseTemplate() {
        return hbaseTemplate;
    }

    public void setHbaseTemplate(HbaseTemplate hbaseTemplate) {
        this.hbaseTemplate = hbaseTemplate;
    }
}
